﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Threading;

using Shared;
using JoltEnvironment.Sql;

namespace LoginServer
{
    public partial class Client
    {
        #region Senders
        
        /// <summary>
        /// Sends a login error message to the client.
        /// </summary>
        /// <param name="Error">Error message ID.</param>
        private void SendError(int Error)
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(MessageHeaders.OUT_ERROR);
            Packet.AddInt32(Error);

            Packet.Send(this.ClientSocket);
        }

        /// <summary>
        /// Sends the server list to the client.
        /// </summary>
        private void SendServerList()
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(MessageHeaders.OUT_SERVER_LIST);

            // Neuz authorization key.
            Packet.AddInt32(0x5d7f1f5c);

            // Account flag.
            Packet.AddByte((byte)'1');

            DataTable Data = null;

            using (SqlDatabaseClient Client = Program.DatabaseManager.GetClient())
            {
                Data = Client.ReadDataTable("SELECT * FROM server_tbl;");
            }

            if (Data != null && Data.Rows.Count > 0)
            {
                DataRow[] Rows = Data.Select();

                Packet.AddInt32(Rows.Length);

                foreach (DataRow ServerData in Rows)
                {
                    // If cluster.
                    if ((int)ServerData["parent_id"] == -1)
                    {
                        Packet.AddInt32(-1); // Parent. (-1 if Cluster.)
                        Packet.AddInt32((int)ServerData["id"]); // ID.
                        Packet.AddString((string)ServerData["name"]); // Name.
                        Packet.AddString((string)ServerData["ip"]); // IP.

                        Packet.AddInt64(0); // Unknown.
                        Packet.AddInt32((int)ServerData["enabled"]); // Enabled.
                        Packet.AddInt32(0);
                    }
                    // Else is channel.
                    else
                    {
                        Packet.AddInt32((int)ServerData["parent_id"]); // Parent. (Cluster id.)
                        Packet.AddInt32((int)ServerData["id"]); // ID.
                        Packet.AddString((string)ServerData["name"]); // Name.
                        Packet.AddInt32(0); // IP.

                        int OnlineCount = 0;

                        using (SqlDatabaseClient Client = Program.DatabaseManager.GetClient())
                        {
                            string Status = ((int)ServerData["parent_id"]).ToString().PadLeft(2, '0') + ((int)ServerData["id"]).ToString().PadLeft(2, '0');

                            Client.AddParameter("status", Status);

                            DataRow Row = Client.ReadDataRow("SELECT COUNT(*) AS online_count FROM account_tbl WHERE status=@status;");

                            if (Row == null)
                            {
                                Program.Logger.Write(Shared.LogType.Warning, "Could not get online count for server: {0}", Status);
                            }
                            else
                            {
                                OnlineCount = int.Parse(Row["online_count"].ToString());
                            }
                        }

                        Packet.AddInt32(1); // Unknown.
                        Packet.AddInt32(OnlineCount); // Online count.
                        Packet.AddInt32((int)ServerData["enabled"]); // Enabled
                        Packet.AddInt32((int)ServerData["online_limit"]); // Online limit.
                    }
                }
            }
            else
            {
                Packet.AddInt32(0);
            }

            Packet.Send(this.ClientSocket);
        }

        #endregion
    }
}
